#if !defined(_DBFRECORDSET_H__INCLUDED_)
#define _DBFRECORDSET_H__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "DBFTableDef.h"
namespace DbfUtility
{
	enum LockModeEnum
	{
		LockPessimistic	= 1,
		LockOptimistic	= 2
	};

	enum LockTypeEnum
	{
		NoLock		= 0,
		LockDBase	= 1,
		LockClipper	= 2,
		LockFoxpro	= 3
	};

	enum EditModeEnum
	{
		noMode,
		edit,
		addnew
	};
}

// CDBFRecordset - the result of a query or base table browse

class TDBFUTILITY_IMPEXP CDBFRecordset : public CObject
{
	DECLARE_DYNAMIC(CDBFRecordset)

// Constructor
public:
	CDBFRecordset(CDBFTableDef* pDBFTable = NULL);

	BOOL Open(LPCTSTR lpszFileName, UINT nOpenFlag = TG_DBF_USE_SHARED);
	void Close();

// Attributes
public:

	BOOL IsOpen() const;
	BOOL IsBOF() const;
	BOOL IsEOF() const;
	BOOL IsDeleted() const;

	CTDbfString GetName();
	CTDbfString GetDBFilePath();

	CTime GetDateCreated();
	CTime GetDateLastUpdated();

	long GetRecordCount();

// Operations
public:
	// Cursor operations
	void MoveNext();
	void MovePrev();
	void MoveFirst();
	void MoveLast();
	void Move(long lRecNumber);

	long GetAbsolutePosition();
	void SetAbsolutePosition(long lOffsetRec);

	// edit buffer operations
	virtual void AddNew();
	virtual void Edit();
	virtual void Update();
	virtual void CancelUpdate();
	virtual void Delete();
	virtual void Undelete();

	// locking
	BOOL LockRecord();
	BOOL UnlockRecord();

	// field operations
	virtual COleVariant GetFieldValue(LPCTSTR lpszName);
	virtual COleVariant GetFieldValue(int nIndex);

	virtual long GetFieldValue(LPCTSTR lpszName,CString& sVal);
	virtual long GetFieldValue(LPCTSTR lpszName,double& dVal);
	virtual long GetFieldValue(LPCTSTR lpszName,float& dVal);
	virtual long GetFieldValue(LPCTSTR lpszName,int& nVal);
	
	virtual long GetFieldValue(int nIndex,CTDbfString& sVal);
	virtual long GetFieldValue(int nIndex,double& dVal);
	virtual long GetFieldValue(int nIndex,int& nVal);

	virtual long GetFieldValue(const char* lpszName, COleVariant& varValue);
	virtual long GetFieldValue(int nIndex, COleVariant& varValue);

	virtual void SetFieldValue(const char* lpszName, const COleVariant& varValue);
	virtual void SetFieldValue(int nIndex, const COleVariant& varValue);

	virtual void SetFieldValue(LPCTSTR lpszName,LPCTSTR pszVal);
	virtual void SetFieldValue(LPCTSTR lpszName,double dVal);
	virtual void SetFieldValue(LPCTSTR lpszName,int nVal);
	
	virtual void SetFieldValue(int nIndex,const char* pszVal);
	virtual void SetFieldValue(int nIndex,double dVal);
	virtual void SetFieldValue(int nIndex,int nVal);

	// Recordset operations
//	virtual void Requery();

	short GetFieldCount();

// Overridables
public:
	virtual CString GetDefaultDBName();

	// for recordset field exchange
//	virtual void DoFieldExchange(CDBFFieldExchange* pFX);

// Implementation
public:
	BOOL ChangeOpenFlag(UINT nNewFlag);
	virtual ~CDBFRecordset();


#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

	CDBFTableDef*	m_pTableDef;
	CDBFFields		m_DBFFields;


protected:
	int m_nOpenType;

	BOOL m_bBOF;			
	BOOL m_bEOF;

	long m_nCurTableRecN;

	UINT m_nLockMode;
	UINT m_nLockType;

	UINT m_nEditMode;

	virtual void ThrowDBFException(long nError = NO_TG_DBF_ERROR);

	BOOL FormatString(const COleVariant& varValue,CTDbfString& Val);

private:
	DBF_UCHAR*	m_pData;
};

//{{AFX_INSERT_LOCATION}}
#endif // !defined(_DBFRECORDSET_H__INCLUDED_)
